<template>
  <div class="app-container">
    <el-card>
      <!--工具栏-->
      <div class="head-container">
        <eHeader :permission="permission"/>
        <crudOperation :permission="permission" />
      </div>
      <!--工具栏-->

      <!--表单组件-->
      <eForm ref="form" />
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column :selectable="checkboxT" type="selection" width="55" />
        <el-table-column v-if="columns.visible('username')" :show-overflow-tooltip="true" prop="username" label="用户名" />
        <el-table-column v-if="columns.visible('nickname')" :show-overflow-tooltip="true" prop="nickname" label="昵称" />
        <el-table-column v-if="columns.visible('sex')" prop="sex" label="性别" >
          <template slot-scope="scope">
            <el-button v-if="scope.row.sex == 0" type="primary" size="mini">女</el-button>
            <el-button v-else-if="scope.row.sex == 1"type="primary" size="mini">男</el-button>
            <el-button v-else="scope.row.sex == 2"type="primary" size="mini">未知</el-button>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('phoneNum')" :show-overflow-tooltip="true" prop="phoneNum" width="100" label="电话" />

        <el-table-column v-if="columns.visible('enabled')" label="状态" align="center" prop="enabled">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enabled"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled(scope.row, scope.row.enabled)"
            />
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('addTime')" :show-overflow-tooltip="true" prop="addTime" width="140" label="创建日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.addTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column  label="操作" width="150px" align="center">
            <template slot-scope="scope">
              <udOperation
                :data="scope.row"
                :permission="permission"
              />
            </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </el-card>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import crudStudent from '@/api/ceamStudent'
import eForm from './form'
import eHeader from './header'
import CRUD, { presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'

// crud交由presenter持有
const crud = CRUD({
  title: '学员',
  url: 'api/student',
  sort: ['sort,asc', 'id,desc'],
  crudMethod: { ...crudStudent }
})

export default {
  components: { eHeader, eForm, crudOperation, pagination, udOperation },
  mixins: [presenter(crud)],
  data() {
    return {
      delLoading: false,
      permission: {
        add: ['admin', 'book:add'],
        edit: ['admin', 'student:edit'],
        del: ['admin', 'student:del']
      }
    }
  },
  created() {
    this.$nextTick(() => {
      //this.init()
    })
  },
  methods: {
    checkPermission,

    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.form = {
        id: data.id,
        routeCn: data.routeCn,
        name: data.name,
        startPoint: data.startPoint,
        endPoint: data.endPoint,
        description: data.description
      }
      _this.dialog = true
    }
  }
}
</script>

<style scoped>

</style>
